package com.clever.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clever.bean.model.OnlineUser;
import com.clever.bean.shopping.projo.output.ConversationDetailVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.clever.mapper.ConversationMapper;
import com.clever.bean.shopping.Conversation;
import com.clever.service.ConversationService;

import javax.annotation.Resource;

/**
 * 会话表服务
 *
 * @Author xixi
 * @Date 2024-04-09 09:09:11
 */
@Service
public class ConversationServiceImpl implements ConversationService {

    private final static Logger log = LoggerFactory.getLogger(ConversationServiceImpl.class);

    @Resource
    private ConversationMapper conversationMapper;

    /**
     * 发起会话
     *
     * @param receiverId 接收者
     * @param productId  商品id
     */
    @Override
    public void launch(String receiverId, String productId, OnlineUser onlineUser) {
        Conversation receiverConversation = new Conversation();
        receiverConversation.setProductId(productId);
        receiverConversation.setReceiverId(receiverId);
        receiverConversation.setSenderId(onlineUser.getId());


        Conversation senderConversation = new Conversation();
        senderConversation.setProductId(productId);
        senderConversation.setSenderId(receiverId);
        senderConversation.setReceiverId(onlineUser.getId());

        conversationMapper.insert(receiverConversation);
        conversationMapper.insert(senderConversation);

    }

    /**
     * 我的会话列表
     *
     * @return List<ConversationDetailVO>
     */
    @Override
    public List<ConversationDetailVO> myConversation(String userId) {
        return conversationMapper.selectConversationDetailByUserId(userId);
    }

    /**
     * 分页查询会话表列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     * @param senderId   发送者
     * @param receiverId 接收者
     * @param productId  发起会话商品
     * @return Page<Conversation>
     */
    @Override
    public Page<Conversation> selectPage(Integer pageNumber, Integer pageSize, String senderId, String receiverId, String productId) {
        QueryWrapper<Conversation> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(senderId)) {
            queryWrapper.eq("sender_id", senderId);
        }
        if (StringUtils.isNotBlank(receiverId)) {
            queryWrapper.eq("receiver_id", receiverId);
        }
        if (StringUtils.isNotBlank(productId)) {
            queryWrapper.eq("product_id", productId);
        }
        return conversationMapper.selectPage(new Page<Conversation>(pageNumber, pageSize), queryWrapper);
    }

    /**
     * 根据id获取会话表
     *
     * @param id id
     * @return Conversation 会话表信息
     */
    @Override
    public Conversation selectById(String id) {
        return conversationMapper.selectById(id);
    }

    /**
     * 根据发送者获取列表
     *
     * @param senderId 发送者
     * @return List<Conversation> 会话表列表
     */
    @Override
    public List<Conversation> selectListBySenderId(String senderId) {
        return conversationMapper.selectList(new QueryWrapper<Conversation>().eq("sender_id", senderId).orderByAsc("id"));
    }

    /**
     * 根据接收者获取列表
     *
     * @param receiverId 接收者
     * @return List<Conversation> 会话表列表
     */
    @Override
    public List<Conversation> selectListByReceiverId(String receiverId) {
        return conversationMapper.selectList(new QueryWrapper<Conversation>().eq("receiver_id", receiverId).orderByAsc("id"));
    }

    /**
     * 根据发起会话商品获取列表
     *
     * @param productId 发起会话商品
     * @return List<Conversation> 会话表列表
     */
    @Override
    public List<Conversation> selectListByProductId(String productId) {
        return conversationMapper.selectList(new QueryWrapper<Conversation>().eq("product_id", productId).orderByAsc("id"));
    }

    /**
     * 新建会话表
     *
     * @param onlineUser 当前登录用户
     * @return Conversation 新建后的会话表信息
     */
    @Override
    public Conversation create(String receiverId, String productId, OnlineUser onlineUser) {
        Conversation conversation = conversationMapper.selectOne(
                new QueryWrapper<Conversation>()
                        .eq("sender_id", onlineUser.getId())
                        .eq("receiver_id", receiverId)
        );
        if (conversation == null) {
            conversation = new Conversation();
            conversation.setReceiverId(receiverId);
            conversation.setSenderId(onlineUser.getId());
            conversation.setProductId(productId);
            conversation.setSessionTime(new Date());
            conversationMapper.insert(conversation);
            log.info("会话表, 会话表信息创建成功: userId={}, conversationId={}", onlineUser.getId(), conversation.getId());
        }else{
            conversation.setProductId(productId);
            conversation.setSessionTime(new Date());
            conversationMapper.updateById(conversation);
            log.info("会话表, 会话表信息修改成功: userId={}, conversationId={}", onlineUser.getId(), conversation.getId());
        }
        return conversation;
    }

    /**
     * 修改会话表
     *
     * @param conversation 会话表实体信息
     * @param onlineUser   当前登录用户
     * @return Conversation 修改后的会话表信息
     */
    @Override
    public Conversation update(Conversation conversation, OnlineUser onlineUser) {
        conversationMapper.updateById(conversation);
        log.info("会话表, 会话表信息修改成功: userId={}, conversationId={}", onlineUser.getId(), conversation.getId());
        return conversation;
    }

    /**
     * 保存会话表
     *
     * @param conversation 会话表实体信息
     * @param onlineUser   当前登录用户
     * @return Conversation 保存后的会话表信息
     */
    @Override
    public Conversation save(Conversation conversation, OnlineUser onlineUser) {
//        if (StringUtils.isNotBlank(conversation.getId())) {
//            return create(conversation, onlineUser);
//        }
        return update(conversation, onlineUser);
    }

    /**
     * 根据id删除会话表信息
     *
     * @param id         id
     * @param onlineUser 当前登录用户
     */
    @Override
    public void delete(String id, OnlineUser onlineUser) {
        conversationMapper.deleteById(id);
        log.info("会话表, 会话表信息删除成功: userId={}, conversationId={}", onlineUser.getId(), id);
    }

    /**
     * 根据id列表删除会话表信息
     *
     * @param ids        id列表
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteBatchIds(List<String> ids, OnlineUser onlineUser) {
        conversationMapper.deleteBatchIds(ids);
        log.info("会话表, 会话表信息批量删除成功: userId={}, count={}, conversationIds={}", onlineUser.getId(), ids.size(), ids.toString());
    }

    /**
     * 根据发送者删除
     *
     * @param senderId   发送者
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteBySenderId(String senderId, OnlineUser onlineUser) {
        conversationMapper.delete(new QueryWrapper<Conversation>().eq("sender_id", senderId));
        log.info("会话表, 会话表信息根据senderId删除成功: userId={}, senderId={}", onlineUser.getId(), senderId);
    }

    /**
     * 根据接收者删除
     *
     * @param receiverId 接收者
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteByReceiverId(String receiverId, OnlineUser onlineUser) {
        conversationMapper.delete(new QueryWrapper<Conversation>().eq("receiver_id", receiverId));
        log.info("会话表, 会话表信息根据receiverId删除成功: userId={}, receiverId={}", onlineUser.getId(), receiverId);
    }

    /**
     * 根据发起会话商品删除
     *
     * @param productId  发起会话商品
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteByProductId(String productId, OnlineUser onlineUser) {
        conversationMapper.delete(new QueryWrapper<Conversation>().eq("product_id", productId));
        log.info("会话表, 会话表信息根据productId删除成功: userId={}, productId={}", onlineUser.getId(), productId);
    }
}
